package com.woniuxy.service.impl;

import com.woniuxy.dao.RbacManagerDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.RbacManagerEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerService;
import com.woniuxy.utils.JWTUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RbacManagerServiceImpl implements RbacManagerService {

    @Autowired(required = false)
    RbacManagerDao rbacManagerDao;

    @Autowired(required = false)
    RedisTemplate redisTemplate;

    @Override
    public int addOne(RbacManagerEntity rbacManagerEntity) {
       return rbacManagerDao.addOne(rbacManagerEntity);
    }

    @Override
    public int delete(int id) {
      return  rbacManagerDao.delete(id);
    }

    @Override
    public List<RbacManager> findAll() {
        return rbacManagerDao.findAll();
    }

    @Override
    public List<RbacManager> findAll(RbacManager rbacManager) {
        return rbacManagerDao.findAllByType(rbacManager);
    }

    @Override
    public int update(RbacManagerEntity rbacManagerEntity) {
        return rbacManagerDao.update(rbacManagerEntity);
    }

    @Override
    public String findPassword(String username) {
        return rbacManagerDao.findPassword(username);
    }

    @Override
    public String sign(String username,String password) {
        long l = System.currentTimeMillis();
        //数据库加密 通过账号作 盐 加密方式 只要盐不同最后结果都不同
        SimpleHash md5 = new SimpleHash("MD5", password, username+l);
        String newpassword = md5.toHex();
        return JWTUtil.sign(username, newpassword);
    }

    @Override
    public boolean verifyMyPassword(String username, String password) {
        /*自定义加密算法，你们自己随便写，username是盐 Salt */
        SimpleHash md5 = new SimpleHash("MD5", password, username);
        //加密后的密码转换成16进制
        String newpassword = md5.toHex();
        //调用findPassword方法，去数据库查出密码
        String password1 = findPassword(username);
        //传入的密码加密后 和 数据库的密码进行比较
        if (newpassword.equals(password1)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public RbacManager findManager(String username) {
        return rbacManagerDao.findManager(username);
    }

    @Override
    public boolean saveTokenToRedis(String token, RbacManager rbacManager) {
        //使用String格式的方法，来存储我们的数据
        ValueOperations<String, RbacManager> ops = redisTemplate.opsForValue();
        ops.set(token,rbacManager);
        //放入后我们需要给token设置一个过期时间
        redisTemplate.expire(token,30, TimeUnit.MINUTES);
        RbacManager rbacManager1 = ops.get(token);
        System.out.println("从redis获取的对象是："+rbacManager1.toString());
        return true;
    }

    @Override
    public RbacManager findRbacManagerByTokenFromRedis(String token) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object o = valueOperations.get(token);
        //首先确保o不是空值 然后确保o的对象是RbacManager对象的类型
        if (o!=null ){
            return (RbacManager) o;
        }
        return null;
    }

    @Override
    public Set<String> findAllRolesById(int id) {
       return   rbacManagerDao.findRolesById(id);
    }
}
